package com.tanhua.dubbo.api.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.pojo.User;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "1.0.0")
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Boolean save(UserInfo userInfo) {
        return this.userInfoMapper.insert(userInfo) > 0;
    }

    @Override
    public Boolean update(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        return this.userInfoMapper.update(userInfo, queryWrapper) > 0;
    }

    @Override
    public UserInfo queryByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.userInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserInfo> queryByUserIdList(List<Object> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserInfo> queryByRecommendUser(List<Object> userIdList, Integer age, String city) {
        //添加过滤条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        if (ObjectUtil.isNotEmpty(age)) {
            //年龄小于等于目标值
            // queryWrapper.le("age", age);
        }
        if (ObjectUtil.isNotEmpty(city)) {
            //城市 like 操作
            // queryWrapper.like("city", city);
        }
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserInfo> queryLikeNickName(String nickName,List<Object> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("nick_name", nickName);
        queryWrapper.in("user_id", userIdList);
        return this.userInfoMapper.selectList(queryWrapper);
    }
}
